package com.hwl.lab.controller;

import com.hwl.lab.entity.Area;
import com.hwl.lab.entity.Canhe;
import com.hwl.lab.service.AreaService;
import com.hwl.lab.service.CanheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@CrossOrigin //允许跨域请求
@RestController
public class CanheController {

    @Autowired
    private CanheService canheService;

    //查询所有（分页）
    @GetMapping("/findCanheAll/{page}/{size}")
    public Page<Canhe> findCanheAll(@PathVariable("page") Integer page, @PathVariable("size") Integer size){
        return canheService.findAll(page,size);
    }
    //查询所有不分页
    @GetMapping("/findCanheAll")
    public List<Canhe> findAll(){
        return canheService.findAll();
    }

    @GetMapping("/findByCanheidF/{canheid}")
    public List<Canhe> findByUserid(@PathVariable("canheid") String canheid){
        if (canheService.findByCanheid(canheid) != null) {
            List<Canhe> canhes = new ArrayList<>();
            canhes.add(canheService.findByCanheid(canheid));
            return canhes;
        } else {
            return null;
        }
    }
    //添加，修改
    @PostMapping("/saveCanhe")
    public void saveCanhe(@RequestBody(required=false) Canhe canhe){
        canheService.saveCanhe(canhe);
    }

    //通过账号删除
    @PostMapping("/deleteCanhe/{id}")
    public void deleteCanhe(@PathVariable("id") Long id){
        canheService.deleteById(id);
    }
}
